<?php

namespace App\Handler\Admin\Member\Member;

if (!defined('IN_PX'))
    exit;

use App\Handler\Admin\AbstractCommon;
use App\Tools\Auxi;
use App\Tools\Html;
use Phoenix\Log\Log4p as logger;
use Phoenix\Support\MsgHelper;

/**
 * 修改
 */
class Edit extends AbstractCommon {

    public function processRequest(Array & $context) {
        try{
            $this->db->beginTransaction();
            $this->_pushSetting();

            $exist = $this->db->table('#@__@member')
                ->where('`member_id` = ?')
                ->bind(array($_POST['id']))
                ->exists();

            if (!$exist) {
                echo(MsgHelper::json('USER_IS_EXISTS', '用户不存在！'));
                exit;
            }

            $count = $this->db->table('`#@__@member`')->where('(`member_name` = ? OR `mobile` = ?) AND member_id != ?')->bind(array($_POST['member_name'], $_POST['mobile'], $_POST['id']))->exists();

            if ($count) {
                echo(MsgHelper::json('USER_IS_EXISTS', '用户名或手机号已存在！'));
                exit;
            }

            $_POST['member_name'] = isset($_POST['member_name']) ? Html::getTextToHtml($_POST['member_name']) : '';
            $_POST['portrait'] = isset($_POST['portrait']) ? Html::getTextToHtml($_POST['portrait']) : '';
            $_POST['location'] = isset($_POST['location']) ? Html::getTextToHtml($_POST['location']) : '';
            $_POST['remarks'] = isset($_POST['remarks']) ? Html::getTextToHtml($_POST['remarks']) : '';
            $_POST['department_id'] = isset($_POST['department_id']) ? intval($_POST['department_id']) : 0;
            $_POST['is_status'] = isset($_POST['is_status']) ? intval($_POST['is_status']) : 0;
            $_POST['sex'] = isset($_POST['sex']) ?  intval($_POST['sex']) : 0;
            $_POST['master_id'] = $this->session->adminUser['id'];
            $_POST['release_date']   = time();
            $_POST['member_id']   = $_POST['id'];

            if (isset($_POST['password']) && $_POST['password'] != '') {//修改密码
                $_POST['guid'] = Auxi::guid();
                $_POST['password'] = md5($_POST['password'] . $_POST['guid']);
                $array = array(
                    'member_name' => '?',
                    'mobile' => '?',
                    'sex' => '?',
                    'portrait' => '?',
                    'department_id' => '?',
                    'location' => '?',
                    'is_status' => '?',
                    'guid' => '?',
                    'password' => '?',
                    'remarks' => '?',
                    'master_id' => '?',
                    'release_date' => '?',
                );
            } else {//不修改密码
                $array = array(
                    'member_name' => '?',
                    'mobile' => '?',
                    'sex' => '?',
                    'portrait' => '?',
                    'department_id' => '?',
                    'location' => '?',
                    'is_status' => '?',
                    'remarks' => '?',
                    'master_id' => '?',
                    'release_date' => '?',
                );
            }

            $_return = $this->db->table('`#@__@member`')
                ->row($array)
                ->where('`member_id` = ?')
                ->bind($_POST)
                ->update();

            $this->db->commit();
            echo(MsgHelper::json(isset($_return) && $_return > 0 ? 'SUCCESS' : 'DB_ERROR'));
        }catch (\Exception $e) {
            $this->db->rollBack();
            return MsgHelper::json('DB_ERROR');
        }
    }

}
